package work.linruchang.util;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.zhxu.bs.*;
import cn.zhxu.bs.implement.DefaultMetaResolver;
import cn.zhxu.bs.implement.DefaultParamResolver;
import cn.zhxu.bs.implement.DefaultSqlResolver;
import cn.zhxu.bs.param.FetchType;
import lombok.SneakyThrows;

import java.util.List;
import java.util.Map;

/**
 * @author LinRuChang
 * @version 1.0
 * @date 2023/03/12
 * @since 1.8
 **/
public class EnhanceBeanSearchUtil {

    private static MetaResolver metaResolver = new DefaultMetaResolver();

    private static ParamResolver paramResolver = new DefaultParamResolver();

    /**
     * 获取SQL信息
     *
     * @param beanClass        要检索的 bean 类型
     * @param conditionParaMap 原始检索参数
     * @param fetchTypeNum     查询的SQL类型
     * @param <T>
     * @return
     */
    @SneakyThrows
    public static <T> SearchSql<T> getSearchSql(Class<T> beanClass, Map<String, Object> conditionParaMap, Integer fetchTypeNum) {
        Assert.notNull(beanClass, "数据库Bean对象不存在，请检查");
        fetchTypeNum = ObjUtil.defaultIfNull(fetchTypeNum, FetchType.LIST_ALL);

        BeanMeta<T> beanMeta = metaResolver.resolve(beanClass);


        FetchType fetchType = new FetchType(fetchTypeNum);
        SearchParam searchParam = paramResolver.resolve(beanMeta, fetchType, conditionParaMap);

        SqlResolver sqlResolver = new DefaultSqlResolver();
        return sqlResolver.resolve(beanMeta, searchParam);

    }

    /**
     * 获取SQL信息
     *
     * @param beanClass        要检索的 bean 类型
     * @param conditionParaMap 原始检索参数
     * @param <T>
     * @return
     */
    @SneakyThrows
    public static <T> SearchSql<T> getSearchSql(Class<T> beanClass, Map<String, Object> conditionParaMap) {
        return getSearchSql(beanClass, conditionParaMap, FetchType.LIST_ALL);
    }


    /**
     * 获取完整的SQL语句，包括上参数拼接
     * 注意：非完全准确
     *
     * @param beanClass        要检索的 bean 类型
     * @param conditionParaMap 原始检索参数
     * @param fetchTypeNum     查询的SQL类型
     */
    public static <T> String getCompleteSql(Class<T> beanClass, Map<String, Object> conditionParaMap, Integer fetchTypeNum) {

        SearchSql<T> searchSql = getSearchSql(beanClass, conditionParaMap, fetchTypeNum);

        String listSqlString = searchSql.getListSqlString();
        List<Object> listSqlParams = searchSql.getListSqlParams();

        String sql = StrUtil.replace(listSqlString, "?", "{}");

        Object[] listSqlParamArray = listSqlParams.stream()
                .map(listSqlParam -> {
                    if (listSqlParam instanceof Number) {
                        return listSqlParam;
                    } else {
                        return StrUtil.format("'{}'", listSqlParam);
                    }
                })
                .toArray();

        sql = StrUtil.format(sql, listSqlParamArray);

        return sql;
    }

    /**
     * 获取完整的SQL语句，包括上参数拼接
     * 注意：非完全准确
     *
     * @param beanClass        要检索的 bean 类型
     * @param conditionParaMap 原始检索参数
     */
    public static <T> String getCompleteSql(Class<T> beanClass, Map<String, Object> conditionParaMap) {
        return getCompleteSql(beanClass, conditionParaMap, FetchType.LIST_ALL);
    }

    public static void main(String[] args) {


    }


}
